package com.xintonghua.bussiness.ui.user.manage;

import android.os.Bundle;
import android.support.design.widget.TabLayout;

import com.alibaba.fastjson.JSONObject;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.xintonghua.base.utils.RefreshUtils;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.adapter.EmployeeAdapter;
import com.xintonghua.bussiness.base.BaseActivity;
import com.xintonghua.bussiness.bean.StaffBean;
import com.xintonghua.bussiness.bean.StaffManagerBean;
import com.xintonghua.bussiness.util.MyState;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 作者：wanxianyi
 * 时间：2018/4/2.
 * 包名：com.xintonghua.bussiness.ui.user.manage
 */

public class SelectEmployeeActivity extends BaseActivity {
    @BindView(R.id.tab_employee)
    TabLayout tabEmployee;
    @BindView(R.id.lv_employee)
    XRecyclerView lvEmployee;
    Unbinder unbinder;
    private EmployeeAdapter adapter;
    private int positionId = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_employee_manage);
        ButterKnife.bind(this);
        onSimpleActionBar();
        setSimpleActionBar("选择员工");
        initUI();
    }

    @Override
    public void initUI() {
        super.initUI();
        RefreshUtils.initGrid(this, lvEmployee);
        lvEmployee.setLoadingListener(this);
        httpCent.staffTypes(this, MyState.requestOne);
        find();
        tabEmployee.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                StaffBean bean = (StaffBean) tab.getTag();
                positionId = bean.getId();
                find();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

    }

    private void find() {
        httpCent.staffManager(positionId, this, MyState.requestTwo);
    }

    @Override
    public void dataBack(Object data, int order) {
        super.dataBack(data, order);
        switch (order) {
            case MyState.requestOne:
                List<StaffBean> list = JSONObject.parseArray((String) data, StaffBean.class);
                list.add(0, new StaffBean("全部", -1));
                for (StaffBean bean : list) {
                    tabEmployee.addTab(tabEmployee.newTab().setText(bean.getName()).setTag(bean));
                }

                break;
            case MyState.requestTwo:
                List<StaffManagerBean> beanList = JSONObject.parseArray((String) data, StaffManagerBean.class);
                adapter = new EmployeeAdapter(this, beanList);
                adapter.setType(1);
                lvEmployee.setAdapter(adapter);
                lvEmployee.refreshComplete();
                break;
        }
    }

    @Override
    public void onRefresh() {
        find();
    }
}
